#include <iostream>
#include <limits>
#include "support.h"
#include <string.h>
#include <memory.h>
#include <math.h>

using namespace std;

int count;
struct FrameHeader
{
     uint16_t header;
     uint16_t len;
     uint8_t id;
     uint8_t type;
     uint8_t ack;
     uint8_t reserve; //
     uint16_t crc;
     uint8_t data[0];
} frameHeader;
struct FrameData
{
     uint16_t msgId;
     uint8_t len;
     uint8_t data[0];
} __attribute__((packed));

void printTestA(string value)
{
     cout << "Test : " << value << endl;
}
enum Color : int32_t
{
     read = 0x00100000,
     green = 0x00100000,
     blue = 0x00400000
};

unsigned short calcCheckSum(unsigned char *p, unsigned short n)
{
     unsigned short sum = 0;
     while (n--)
     {
          sum += *p++;
     }
     return sum;
}

int main()
{
     short int i;
     short unsigned int j;
     j = 50000;
     i = j;
     cout << i << "  " << j << endl;

     count = 5;
     // write_extern();

     short int &reset = i;
     short unsigned int reset2 = j;
     cout << "Value of i : " << i << endl;
     cout << "Value of i reference : " << reset << endl;

     cout << "Value of j : " << j << endl;
     cout << "Value of j reference : " << reset2 << endl;

     short int testR = 500;
     reset = testR;
     cout << "Value of testR : " << testR << endl;
     cout << "Value of testR reference : " << reset << endl;

     bool isDisposable = 1 == 2;
     cout << "VehicleObjectPool : isDisposable = " << (isDisposable ? "true" : "false") << endl;
     // printf("VehicleObjectPool : isDisposable = "+isDisposable?"true":"false");

     isDisposable ? printTestA("张三") : printTestA("李四");

     Color color = Color::blue;
     size_t vecValue = 100;
     auto colorKey = static_cast<int32_t>(color);
     auto vecValueKey = static_cast<int32_t>(vecValue);
     int32_t key = colorKey | vecValueKey;

     uint32_t mid = 524296;
     if (mid == 0x2FD || mid == 765)
     {
          printf("zoulx debug : %s:%d HVAC_FAN_SPEED mid = (%d)\n", __FUNCTION__, __LINE__, mid);
     }
     else
     {
          printf("zoulx debug :\n");
     }

     cout << "color key : " << colorKey << "  vecValueKey : " << vecValueKey << "  key : " << key << endl;

     {
          cout << "type: \t\t"
               << "************size**************" << endl;
          cout << "bool: \t\t"
               << "所占字节数：" << sizeof(bool);
          cout << "\t最大值：" << (numeric_limits<bool>::max)();
          cout << "\t\t最小值：" << (numeric_limits<bool>::min)() << endl;
          cout << "char: \t\t"
               << "所占字节数：" << sizeof(char);
          cout << "\t最大值：" << (numeric_limits<char>::max)();
          cout << "\t\t最小值：" << (numeric_limits<char>::min)() << endl;
          cout << "signed char: \t"
               << "所占字节数：" << sizeof(signed char);
          cout << "\t最大值：" << (numeric_limits<signed char>::max)();
          cout << "\t\t最小值：" << (numeric_limits<signed char>::min)() << endl;
          cout << "unsigned char: \t"
               << "所占字节数：" << sizeof(unsigned char);
          cout << "\t最大值：" << (numeric_limits<unsigned char>::max)();
          cout << "\t\t最小值：" << (numeric_limits<unsigned char>::min)() << endl;
          cout << "wchar_t: \t"
               << "所占字节数：" << sizeof(wchar_t);
          cout << "\t最大值：" << (numeric_limits<wchar_t>::max)();
          cout << "\t\t最小值：" << (numeric_limits<wchar_t>::min)() << endl;
          cout << "short: \t\t"
               << "所占字节数：" << sizeof(short);
          cout << "\t最大值：" << (numeric_limits<short>::max)();
          cout << "\t\t最小值：" << (numeric_limits<short>::min)() << endl;
          cout << "int: \t\t"
               << "所占字节数：" << sizeof(int);
          cout << "\t最大值：" << (numeric_limits<int>::max)();
          cout << "\t最小值：" << (numeric_limits<int>::min)() << endl;
          cout << "unsigned: \t"
               << "所占字节数：" << sizeof(unsigned);
          cout << "\t最大值：" << (numeric_limits<unsigned>::max)();
          cout << "\t最小值：" << (numeric_limits<unsigned>::min)() << endl;
          cout << "long: \t\t"
               << "所占字节数：" << sizeof(long);
          cout << "\t最大值：" << (numeric_limits<long>::max)();
          cout << "\t最小值：" << (numeric_limits<long>::min)() << endl;
          cout << "unsigned long: \t"
               << "所占字节数：" << sizeof(unsigned long);
          cout << "\t最大值：" << (numeric_limits<unsigned long>::max)();
          cout << "\t最小值：" << (numeric_limits<unsigned long>::min)() << endl;
          cout << "double: \t"
               << "所占字节数：" << sizeof(double);
          cout << "\t最大值：" << (numeric_limits<double>::max)();
          cout << "\t最小值：" << (numeric_limits<double>::min)() << endl;
          cout << "long double: \t"
               << "所占字节数：" << sizeof(long double);
          cout << "\t最大值：" << (numeric_limits<long double>::max)();
          cout << "\t最小值：" << (numeric_limits<long double>::min)() << endl;
          cout << "float: \t\t"
               << "所占字节数：" << sizeof(float);
          cout << "\t最大值：" << (numeric_limits<float>::max)();
          cout << "\t最小值：" << (numeric_limits<float>::min)() << endl;
          cout << "size_t: \t"
               << "所占字节数：" << sizeof(size_t);
          cout << "\t最大值：" << (numeric_limits<size_t>::max)();
          cout << "\t最小值：" << (numeric_limits<size_t>::min)() << endl;
          cout << "string: \t"
               << "所占字节数：" << sizeof(string) << endl;
          // << "\t最大值：" << (numeric_limits<string>::max)() << "\t最小值：" << (numeric_limits<string>::min)() << endl;
          cout << "type: \t\t"
               << "************size**************" << endl;
     }

     {
          int buf[100];
          cout << "arry 1024 sizeof = " << sizeof(buf) << endl;
     }

     char mBuf[2048] = "oldstring3";
     char src[] = "newstring";

     int mPos = 1024;
     int frameLen = 3;

     printf("Before memmove dest = %s, src = %s\n", mBuf, src);
     memmove(mBuf, mBuf + frameLen, mPos - frameLen);
     printf("After memmove dest = %s, src = %s\n", mBuf, src);

     {
          uint8_t data[10];
          {
               cout << "数组大小 : data[0]：" << sizeof(data) << endl;
               cout << "frame header : size = " << sizeof(frameHeader) << endl;
          }

          uint8_t *mSendBuf;

          FrameHeader *frame = new FrameHeader();

          uint8_t *p = frame->data;
          FrameData *fd;
          frame->header = 0xBBAA;
          frame->type = 0x20;
          frame->len = sizeof(FrameHeader);
          uint16_t left = 2048 - 512;

          int dataSize = 16;
          uint16_t sum;
          {
               for (int i = 0; i < dataSize; i++)
               {
                    data[i] = 0x1 * i;
               }
               memcpy(frame->data, data, dataSize);
          }
          p = (uint8_t *)frame->data;
          {
               sum = calcCheckSum(data, dataSize);
               cout << "sum value = " << sum << endl;
          }
          {
               char str[256] = {0};
               uint16_t len = dataSize;
               for (unsigned i = 0, pos = 0; pos < sizeof(str) && i < len; ++i)
               {
                    pos += snprintf(str + pos, sizeof(str) - pos, " %02x", data[i]);
               }
               cout << "sum value = " << sum << " data value =" << str << endl;
          }
          {
               fd = (FrameData *)p;
               {
                    char str[256] = {0};
                    uint16_t len = dataSize;
                    for (unsigned i = 0, pos = 0; pos < sizeof(str) && i < len; ++i)
                    {
                         pos += snprintf(str + pos, sizeof(str) - pos, " %02x", p[i]);
                    }
                    cout << "sum value = " << sum << " data value =" << str << endl;
               }
               frame->len += sizeof(FrameData);
               left -= sizeof(FrameData);
               p += sizeof(FrameData);
               {
                    left -= fd->len;
                    frame->len += fd->len;
                    p += fd->len;
               }
               {
                    frame->len += 2;
                    sum = calcCheckSum(frame->data, frame->len - sizeof(FrameHeader) - 2);
                    int offset = frame->len - sizeof(frameHeader) - 2;
                    //加上两位校验和
                    memcpy(data + offset, &sum, 2);
                    cout << "offset = " << offset << endl;
               }
          }
          {
               char str[256] = {0};
               uint16_t len = dataSize;
               for (unsigned i = 0, pos = 0; pos < sizeof(str) && i < len; ++i)
               {
                    pos += snprintf(str + pos, sizeof(str) - pos, " %02x", data[i]);
               }
               cout << "sum value = " << sum << " data value =" << str << endl;
          }
     }

     {

          char *data = "1234张三李四";
          int jsonContent = *((int *)data);
          cout << "jsonContet = " << jsonContent << endl;
     }

     {

          char str[80];

          sprintf(str, "Pi 的值 = %f \n", M_PI);
          cout << "str value \n{\n " << str << "}"<<endl;

     }
     return 0;
}
